package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.attribute.Link;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element tocItem is a component of the table of content and contains header information about sections or parts of
 * the rest of the document.
 * 
 * <pre>
 * <xsd:element name="tocItem">
 * 		<xsd:complexType mixed="true">
 * 			<xsd:complexContent>
 * 				<xsd:extension base="inline">
 * 					<xsd:attributeGroup ref="link"/>
 * 					<xsd:attribute name="level" type="xsd:integer" use="required"/>
 * 				</xsd:extension>
 *			</xsd:complexContent>
 *		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class TocItem extends InlineType implements Link, Externalizable{

	public static final String ELEMENT = "tocItem";
	
	public static final String ATTRIBUTE_LEVEL = "level";
		
	// Mandatory
	private String _href;
	
	// Mandatory
	private Integer _level;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Link#getHref()
	 */
	public String getHref() {
		return _href;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Link#setHref(java.lang.String)
	 */
	public void setHref(String href) {
		_href = href;
	}

	public Integer getLevel() {
		return _level;
	}

	public void setLevel(Integer level) {
		_level = level;
	}
	
	/**
	 * @see org.akomantoso.core.data.InlineType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <E extends Output> void writeExternal(E out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeLink(out, this);
		OutputUtil.writeMandatoryAttribute(out, ATTRIBUTE_LEVEL, _level.toString(), this);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.InlineType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <E extends Input> void readExternal(E in) throws IOException {
		_href = in.readAttribute(Link.ATTRIBUTE);
		_level = Integer.valueOf(in.readAttribute(ATTRIBUTE_LEVEL));
		super.readExternal(in);
	}

	/**
	 * @see ec.ep.visitor.AKVisitable#accept(ec.ep.visitor.AKVisitor)
	 */
	public void accept(AKVisitor visitor) {
		if (visitor.visitBegin(this)) {
			internalAccept(visitor);
			visitor.visitEnd(this);
		}
	}
	
}